package gaml.testing;

import static org.junit.Assert.assertTrue;

import gaml.core.GamlConfig;
import gaml.error.GamlException;

import gaml.java.ClassSerializerStore;

import gaml.java.DefaultSerializerStoreFactory;
import gaml.java.NodeObjectBridge;

import gaml.text.DefaultParserStoreFactory;
import gaml.text.DefaultPrinterStoreFactory;

import gaml.text.TextNodeBridge;

import gaml.text.YamlText;

import gaml.yaml.YamlNode;

import gaml.yaml.ahchor.YamlAnchorCodeStore;
import gaml.yaml.ahchor.YamlAnchorObjectStore;

import java.util.HashSet;

import org.junit.Test;

public class HashSetSerializerTest {

	@Test
	public void testSerialize () throws GamlException {
//		System.out.println (getSerializedText ());
		
		String res = getSerializedText ();
		
		assertTrue (res.contains ("!java.lang.String > Set Value 1\n: \n"));
		assertTrue (res.contains ("!java.lang.String > Set Value 2\n: \n"));
	}

	@Test
	public void testRestore () throws GamlException {
		YamlText text = new YamlText ().read (getSerializedText ());
		
		YamlAnchorCodeStore anchors = new YamlAnchorCodeStore ();
		YamlNode node = new TextNodeBridge (text, null).parseText (DefaultParserStoreFactory.getStore (anchors)).getNode ();
		HashSet <String> set = NodeObjectBridge.create (node, HashSet.class, DefaultSerializerStoreFactory.getStore ()).parseNode (anchors).getObject ();
		
		assertTrue (set.contains ("Set Value 1"));
		assertTrue (set.contains ("Set Value 2"));
	}

	protected String getSerializedText () throws GamlException {
		HashSet <String> set = new HashSet <String> ();
		set.add ("Set Value 1");
		set.add ("Set Value 2");
		
		ClassSerializerStore store = DefaultSerializerStoreFactory.getStore ();
		YamlNode node = NodeObjectBridge.create (set, null, HashSet.class, store).parseObject (new YamlAnchorObjectStore ()).getNode ();
		
		return new TextNodeBridge (new YamlText (), node).parseNode (DefaultPrinterStoreFactory.getStore (GamlConfig.getDefault ())).getText ().getText ();
	}
}
